package com.eqdd.yiqidian.ui.companyspace

import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.BGABannerAdapter
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.EquipmentBean
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.kotlin.deleteDialog
import com.eqdd.yiqidian.kotlin.imgPathPreview
import com.eqdd.yiqidian.utils.ToastUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_equipment_info.*
import org.jetbrains.anko.toast

/**
 * Created by jhj on 18-6-30.
 */
class EquipmentInfoActivity : BaseActivity() {

    private var equipmentBean: EquipmentBean? = null

    override fun initBinding() {
        setContentView(R.layout.activity_equipment_info)
        initTopTitleBar(View.VISIBLE, "设备详情")
        val type = intent.getIntExtra(Config.TYPE, 0)
        if (type == 0) {
            initTopRightText("删除") {
                if (equipmentBean != null) {
                    deleteDialog(HttpConfig.BASE_URL + HttpConfig.EQUIPMENT_DELETE, "是否删除${equipmentBean?.equipmentName ?: "该数据"}？", "equipmentId" to equipmentBean?.id)
                } else {
                    toast("删除失败")
                }

            }
        }
    }

    override fun initData() {
        val id = intent.getIntExtra(Config.ID, -1)
        if (id == -1) {
            ToastUtil.showShort("没有数据")
            finish()
            return
        }
        OkGo.post<HttpResult<EquipmentBean>>(HttpConfig.BASE_URL + HttpConfig.EQUIPMENT_INFO)
                .params("userGuid", user.guid)
                .params("companyId", user.companyId)
                .params("equipmentId", id)
                .execute(object : DialogCallBack<HttpResult<EquipmentBean>>(this) {
                    override fun onSuccess(response: Response<HttpResult<EquipmentBean>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            equipmentBean = result.items
                            setView()
                        } else {
                            toast("获取详情失败")
                            finish()
                        }
                    }

                })
    }

    override fun setView() {
        equipmentBean?.let {
            richEditor.setInputEnabled(false)
            richEditor.setPadding(10, 0, 10, 0)
            val html = it.equipmentMsg
                    .replace("<pre>", "")
                    .replace("</pre>", "")
                    .replace("<br>", "")
            richEditor.html = html
            tv_name.text = it.equipmentName
            tv_product.text = it.manufactor
            tv_date.text = it.dateOfPurchase

            banner_viewpager.setAdapter(BGABannerAdapter(this))
            banner_viewpager.setData(it.images, null)
            banner_viewpager.setDelegate { banner, itemView, model, position ->
                imgPathPreview(it.images, position)
            }
        }
    }

}